var navigationRule = {
	"pageId" : "contact"
};

function init() {
	_initHeader();
	
	$("#updateButton").unbind("click");
	$("#removeButton").unbind("click");
	$("#updateButton").click(clickedUpdateButton);
	$("#removeButton").click(function(e) {
		contact.remove(navigation.parameters.contactId, removeContactSuccessCallback, removeContactErrorCallback);
	});
	
	contact.get(navigation.parameters.contactId, getContactSuccessCallback, getContactErrorCallback);
}

function getContactSuccessCallback(contactInfo) {
	if(contactInfo.firstName != "" || contactInfo.firstName != undefined || contactInfo.firstName != null) {
		$("#firstName").val(contactInfo.firstName);
	}
	
	if(contactInfo.lastName != "" || contactInfo.lastName != undefined || contactInfo.lastName != null) {
		$("#lastName").val(contactInfo.lastName);
	}
	
	if(contactInfo.mobilePhone != "" || contactInfo.mobilePhone != undefined || contactInfo.mobilePhone != null) {
		$("#mobilePhone").val(contactInfo.mobilePhone);
	}
	
	if(contactInfo.workPhone != "" || contactInfo.workPhone != undefined || contactInfo.workPhone != null) {
		$("#workPhone").val(contactInfo.workPhone);
	}
	
	if(contactInfo.email != "" || contactInfo.email != undefined || contactInfo.email != null) {
		$("#email").val(contactInfo.email);
	}
	
	if(contactInfo.organization != "" || contactInfo.organization != undefined || contactInfo.organization != null) {
		$("#organization").val(contactInfo.organization);
	}
	
	if(contactInfo.department != "" || contactInfo.department != undefined || contactInfo.department != null) {
		$("#department").val(contactInfo.department);
	}
	
	if(contactInfo.jobTitle != "" || contactInfo.jobTitle != undefined || contactInfo.jobTitle != null) {
		$("#jobTitle").val(contactInfo.jobTitle);
	}
}

function getContactErrorCallback(errorMessage) {
	alert("getContactErrorCallback! Error message : " + errorMessage);
}

function clickedUpdateButton() {
	if($("#firstName").val() == "" || $("#firstName").val() == undefined || $("#firstName").val() == null) {
		alert("First name is required.");
		return false;
	}
	
	if($("#lastName").val() == "" || $("#lastName").val() == undefined || $("#lastName").val() == null) {
		alert("Last name is required.");
		return false;
	}
	
	var contactInfo = {
		"contactId" : navigation.parameters.contactId,
		"firstName" : $("#firstName").val(),
		"lastName" : $("#lastName").val(),
		"mobilePhone" : $("#mobilePhone").val(),
		"workPhone" : $("#workPhone").val(),
		"email" : $("#email").val(),
		"organization" : $("#organization").val(),
		"department" : $("#department").val(),
		"jobTitle" : $("#jobTitle").val()
	};
	
	contact.update(contactInfo, updateContactSuccessCallback, updateContactErrorCallback);
}

/*
 * Update Contact
 */
function updateContactSuccessCallback() {
	notifySuccess("update");
	navigation.backToOrNavigate(navigationRule);
}

function updateContactErrorCallback(errorMessage) {
	notifyError("update", errorMessage);
}

/*
 * Remove Contact
 */
function removeContactSuccessCallback() {
	notifySuccess("remove");
	navigation.backToOrNavigate(navigationRule);
}

function removeContactErrorCallback(errorMessage) {
	notifyError("remove", errorMessage);
}

function onScreenBack() {
	log.log("onScreenBack on conatct_update page is called.");
}
